package bdeenyy.threevector.controller;

import bdeenyy.threevector.Main;
import javafx.geometry.Insets;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.Tab;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;

import java.io.InputStream;

public class ResultTabController {
    private GridPane drawingGridPane;

    public Tab createResultTab() {
        Tab tab = new Tab("Результат");
        tab.setClosable(true);
        tab.setOnClosed(event -> Main.setResultTabClosed(true));

        drawingGridPane = new GridPane();
        drawingGridPane.setPadding(new Insets(10));
        drawingGridPane.setHgap(20); // Расстояние между столбцами
        drawingGridPane.setVgap(20); // Расстояние между строками
        drawingGridPane.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));

        ScrollPane drawingScrollPane = new ScrollPane(drawingGridPane);
        drawingScrollPane.setPannable(true);
        drawingScrollPane.setFitToWidth(true);
        drawingScrollPane.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));

        InputStream imageStream = getClass().getResourceAsStream("/images/rose.png");
        assert imageStream != null;
        Image roseImage = new Image(imageStream);
        ImageView roseImageView = new ImageView(roseImage);
        roseImageView.setFitWidth(40);
        roseImageView.setFitHeight(200);

        StackPane logoPane = new StackPane(roseImageView);
        logoPane.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
        logoPane.setPadding(new Insets(10));

        HBox resultContent = new HBox(10, logoPane, drawingScrollPane);
        resultContent.setPadding(new Insets(10));
        resultContent.setBackground(new Background(new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY)));
        tab.setContent(resultContent);

        return tab;
    }

    public void clearDrawingGridPane() {
        drawingGridPane.getChildren().clear();
    }

    public void addDrawingPane(Pane drawingPane, int column, int row) {
        drawingGridPane.add(drawingPane, column, row);
    }
}
